package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.OrderSettingsDao;
import cn.itcast.entity.OrderSettingVo;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingsService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingsServiceImpl implements OrderSettingsService {

    @Autowired
    private OrderSettingsDao orderSettingsDao;
    /**根据日期查询预约列表*/
    @Override
    public List<OrderSettingVo> getOrderSettingsByMonth(String date) {
        try {
            //格式转换
            Date min = DateUtils.parseString2Date(date, "yyyy-MM");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(min);
            calendar.add(Calendar.MONTH,1);
            //获取maxTime
            Date max = calendar.getTime();
            //转换成字符串
            String minDate = DateUtils.parseDate2String(min);
            String maxDate = DateUtils.parseDate2String(max);
            //获取设置列表
            List<OrderSetting> orderSettingList = orderSettingsDao.getOrderSettingsByMonth(minDate, maxDate);
            if (null==orderSettingList){
                return null;
            }
            //封装成Vo对象
            List<OrderSettingVo> orderSettingVoList = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                OrderSettingVo orderSettingVo = new OrderSettingVo();
                //转换成标准日期用于计算
                calendar.setTime(orderSetting.getOrderDate());
                orderSettingVo.setDate(calendar.get(Calendar.DAY_OF_MONTH));//设置日期
                orderSettingVo.setNumber(orderSetting.getNumber());//设置可预约人数
                orderSettingVo.setReservations(orderSetting.getReservations());//设置已预约人数
                orderSettingVoList.add(orderSettingVo);
            }
            return orderSettingVoList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**更新可预约人数数据*/
    @Override
    public Boolean updateOrderSettingNumber(OrderSetting orderSetting) {

        try {
            //1.查询已预约人数,如果已预约人数大于可预约人数,则设置失败
            Integer reservations = this.queryReservationsByOrderDate(orderSetting.getOrderDate());
            Integer number = orderSetting.getNumber();
            if (reservations>number){
                //已预约大于可预约,更新失败
                return false;
            }
            //2.日期转换成字符串,更新设置
            String orderDate = DateUtils.parseDate2String(orderSetting.getOrderDate());
            return orderSettingsDao.updateOrderSettingNumber(orderDate,number);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**设置数据*/
    @Override
    public Boolean saveOrderSettingNumber(OrderSetting orderSetting) {
        return orderSettingsDao.saveOrderSettingNumber(orderSetting);
    }

    /**查询已预约人数*/
    @Override
    public Integer queryReservationsByOrderDate(Date orderDate) {
        //转换成日期,用于查询mysql
        try {
            String date = DateUtils.parseDate2String(orderDate);
            Integer reservations = orderSettingsDao.queryReservationsByOrderDate(date);
            if (null==reservations){
                //查询为空(不是0)
                return null;
            }
            return reservations;
        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }

    /**是否是第一次设置*/
    @Override
    public Boolean isFirstOrderSetting(Date orderDate) {
        Integer reservations = this.queryReservationsByOrderDate(orderDate);
        if (reservations==null){
            //查询为空,第一次注册
            return true;
        }
        //不是第一次设置
        return false;
    }

    /**更新已预约人数*/
    @Override
    public Boolean updateOrderSettingReservations(String orderDate,Integer reservations) {
        return orderSettingsDao.updateOrderSettingReservations(orderDate,reservations);
    }

    /**查询单日的预约情况*/
    public OrderSetting queryOrderSettingByOrderDate(String orderDate){
        return orderSettingsDao.queryOrderSettingByOrderDate(orderDate);
    }










}
